package co.baiku.boot.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;

@ConditionalOnProperty("ajavaer.weixin.sns.appid")
@ConfigurationProperties(prefix = "ajavaer.weixin.sns")
public class WeixinSnsConfig {
    private String appid;
    private String secret;


    public WeixinSnsConfig() {
    }

    public WeixinSnsConfig(WeixinPublicConfig weixinPublicConfig) {
        this.appid = weixinPublicConfig.getAppid();
        this.secret = weixinPublicConfig.getSecret();
    }

    public WeixinSnsConfig(WeixinOpenConfig weixinOpenConfig) {
        this.appid = weixinOpenConfig.getAppid();
        this.secret = weixinOpenConfig.getSecret();
    }

    public WeixinSnsConfig(WeixinMiniProgramConfig weixinMiniProgramConfig) {
        this.appid = weixinMiniProgramConfig.getAppid();
        this.secret = weixinMiniProgramConfig.getSecret();
    }

    private String sns_access_token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code";
    private String sns_code_url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=%s&redirect_uri=%s&response_type=code&scope=%s&state=%s#wechat_redirect";
    private String check_sns_access_token_url = "https://api.weixin.qq.com/sns/auth?access_token=%s&openid=%s";
    private String refresh_token_url = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=%s&grant_type=%s&refresh_token=REFRESH_TOKEN";
    private String userinfo_url = "https://api.weixin.qq.com/sns/userinfo?access_token=%s&openid=%s&lang=zh_CN";
    private String sns_website_code_url = "https://open.weixin.qq.com/connect/qrconnect?appid=%s&redirect_uri=%s&response_type=code&scope=snsapi_login&state=%s#wechat_redirect";

    public String getSns_code_url(String redirect_uri, String scope, String state) {
        return String.format(sns_code_url, this.appid, redirect_uri, scope, state);
    }

    public String getSns_access_token_url(String code) {
        return String.format(sns_access_token_url, this.appid, this.secret, code);
    }

    public String getCheck_sns_access_token_url(String access_token, String openid) {
        return String.format(check_sns_access_token_url, access_token, openid);
    }

    public String getRefresh_token_url(String grant_type) {
        return String.format(refresh_token_url, this.appid, grant_type);
    }

    public String getUserinfo_url(String access_token, String openid) {
        return String.format(userinfo_url, access_token, openid);
    }

    public String getSns_website_code_url(String redirect_uri, String state) {
        return String.format(sns_website_code_url, this.appid, redirect_uri, state);
    }

    public String getAppid() {
        return appid;
    }

    public void setAppid(String appid) {
        this.appid = appid;
    }
}
